package org.will.test.domain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.will.test.util.XML;

import java.io.Serializable;

/**
 * Created by jsycwangwei on 2017/7/9.
 */
public class PayError implements Serializable{
    private int errCode;
    private String errMsg;
    private JSONObject json;
    private String responseContent;

    public PayError(){}

    public PayError(int errCode, String errMsg){
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public PayError(int errCode, String errMsg,String responseContent){
        this(errCode,errMsg);
        this.responseContent = responseContent;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public JSONObject getJson() {
        return json;
    }

    public void setJson(JSONObject json) {
        this.json = json;
    }

    public String getResponseContent() {
        return responseContent;
    }

    public void setResponseContent(String responseContent) {
        this.responseContent = responseContent;
    }

    public static PayError formJson(String json){
        JSONObject jsonObject = JSON.parseObject(json);
        PayError error = jsonObject.toJavaObject(PayError.class);
        error.setJson(jsonObject);
        error.setResponseContent(json);
        return error;
    }

    public static PayError formXml(String xml){
        JSONObject jsonObject = XML.toJsonObject(xml);
        if (null == jsonObject.get("return_code")){
            PayError error = new PayError(403, null == jsonObject.get("return_msg") ? "未知错误！" : jsonObject.get("return_msg").toString());
            return error;
        }

        if ("FAIL".equals( jsonObject.get("return_code"))){
            PayError error = new PayError(-1,  jsonObject.get("return_msg").toString());
            return error;
        }
        return null;
    }
}
